<?php


namespace ImiApp\ApiServer\Api\Controller\task;

use Imi\Log\Log;
use Imi\Bean\Annotation\Bean;
use Imi\Queue\Contract\IMessage;
use Imi\Queue\Driver\IQueueDriver;
use Imi\Queue\Service\BaseQueueConsumer;

use Imi\Db\Db;

/**
 * @Bean("TaskAuction")
 */
class TaskAuction extends BaseQueueConsumer
{
    /**
     * 处理消费
     *
     * @param \Imi\Queue\Contract\IMessage $message
     * @param \Imi\Queue\Driver\IQueueDriver $queue
     * @return void
     */
    protected function consume(IMessage $message, IQueueDriver $queue): void
    {

        // 执行对列的地方
        // Log::info(sprintf('[%s]%s:%s',$message->getMessage(), $queue->getName(), $message->getMessageId()));

        $params = json_decode($message->getMessage(), true);
        $auction = $params['auction'];
        $auction = Db::name('auction')->where('id', '=', $auction['id'])->find();
        switch ($auction['type_status']) {
            case '1':
                $拍卖类型 = '贝壳竞拍';
                break;
            case '2':
                $拍卖类型 = '降价拍';
                break;
            case '3':
                $拍卖类型 = '升价拍';
                break;
            case '4':
                $拍卖类型 = '暗拍(降价拍)';
                break;
            case '5':
                $拍卖类型 = '暗拍(升价拍)';
                break;
            default:
                # code...
                break;
        }

        Log::info($拍卖类型 . '队列' . $auction['id'] . $params['type']);


        switch ($params['type']) {

            case 'update_auction_status1':
                //判断库存是否都有,否则下架

                if ($auction['start_status'] == 'time') {

                    // if($auction['end_time']<=time()){
                    //更新状态
                    $data['status'] = '3'; //状态:1=待展示,2=预展示,3=进行中,5=结束,6=已下架
                    Db::name('auction')->where('status', '=', '2')->where('id', '=', $auction['id'])->update($data);







                    $queue2 = \Imi\Queue\Facade\Queue::getQueue('auction');
                    $message2 = new \Imi\Queue\Model\Message;
                    //发布对列
                    $message2->setMessage(json_encode(['type' => 'auction_finish' . $auction['type_status'], 'auction' => $auction], JSON_UNESCAPED_UNICODE));
                    $queue2->push($message2, $auction['end_time'] - time());






                    //推送开始拍卖
                    //推送到拍卖群组里面信息
                    $auction_start_data = ['auction_id' => $auction['id'], 'end_date' => date('Y-m-d H:i:s', $auction['end_time']), 'type_status' => $auction['type_status']];
                    $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    $ToGroup_data = $tpmecms->ws_success('开始拍卖', $auction_start_data, 'auction_start');
                    \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
                    \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据
                }

                break;
            case 'update_auction_status2':
                //判断库存是否都有,否则下架

                if ($auction['start_status'] == 'time') {

                    // if($auction['end_time']<=time()){
                    //更新状态
                    $data['status'] = '3'; //状态:1=待展示,2=预展示,3=进行中,5=结束,6=已下架
                    Db::name('auction')->where('status', '=', '2')->where('id', '=', $auction['id'])->update($data);







                    $queue2 = \Imi\Queue\Facade\Queue::getQueue('auction');
                    $message2 = new \Imi\Queue\Model\Message;
                    //发布对列
                    $message2->setMessage(json_encode(['type' => 'auction_finish' . $auction['type_status'], 'auction' => $auction], JSON_UNESCAPED_UNICODE));
                    $queue2->push($message2, $auction['end_time'] - time());






                    //推送开始拍卖
                    //推送到拍卖群组里面信息
                    $auction_start_data = ['auction_id' => $auction['id'], 'end_date' => date('Y-m-d H:i:s', $auction['end_time']), 'type_status' => $auction['type_status']];
                    $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    $ToGroup_data = $tpmecms->ws_success('开始拍卖', $auction_start_data, 'auction_start');
                    \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
                    \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据
                }

                break;
            case 'update_auction_status3':
                // $auction = $params['auction'];
                // if($auction['start_status']=='time'){
                //     //更新状态
                // }
                // if($auction['start_status']=='number'){

                // }

                //判断库存是否都有,否则下架

                if ($auction['start_status'] == 'time') {
                    // if($auction['end_time']<=time()){
                    //更新状态
                    $data['status'] = '3'; //状态:1=待展示,2=预展示,3=进行中,5=结束,6=已下架
                    Db::name('auction')->where('status', '=', '2')->where('id', '=', $auction['id'])->update($data);

                    $queue2 = \Imi\Queue\Facade\Queue::getQueue('auction');
                    $message2 = new \Imi\Queue\Model\Message;
                    //发布对列
                    $message2->setMessage(json_encode(['type' => 'auction_finish' . $auction['type_status'], 'auction' => $auction], JSON_UNESCAPED_UNICODE));
                    $queue2->push($message2, $auction['end_time'] - time());


                    //推送开始拍卖
                    //推送到拍卖群组里面信息
                    $auction_start_data = ['auction_id' => $auction['id'], 'end_date' => date('Y-m-d H:i:s', $auction['end_time']), 'type_status' => $auction['type_status']];
                    $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    $ToGroup_data = $tpmecms->ws_success('开始拍卖', $auction_start_data, 'auction_start');
                    \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
                    \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据
                }

                break;



            case 'update_auction_status4':
                // $auction = $params['auction'];
                // if($auction['start_status']=='time'){
                //     //更新状态
                // }
                // if($auction['start_status']=='number'){

                // }

                //判断库存是否都有,否则下架

                if ($auction['start_status'] == 'time') {
                    // if($auction['end_time']<=time()){
                    //更新状态
                    $data['status'] = '3'; //状态:1=待展示,2=预展示,3=进行中,5=结束,6=已下架
                    Db::name('auction')->where('status', '=', '2')->where('id', '=', $auction['id'])->update($data);

                    $queue2 = \Imi\Queue\Facade\Queue::getQueue('auction');
                    $message2 = new \Imi\Queue\Model\Message;
                    //发布对列
                    $message2->setMessage(json_encode(['type' => 'auction_finish' . $auction['type_status'], 'auction' => $auction], JSON_UNESCAPED_UNICODE));
                    $queue2->push($message2, $auction['end_time'] - time());


                    //推送开始拍卖
                    //推送到拍卖群组里面信息
                    $auction_start_data = ['auction_id' => $auction['id'], 'end_date' => date('Y-m-d H:i:s', $auction['end_time']), 'type_status' => $auction['type_status']];
                    $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    $ToGroup_data = $tpmecms->ws_success('开始拍卖', $auction_start_data, 'auction_start');
                    \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
                    \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据
                }

                break;


            case 'update_auction_status5':
                // $auction = $params['auction'];
                // if($auction['start_status']=='time'){
                //     //更新状态
                // }
                // if($auction['start_status']=='number'){

                // }

                //判断库存是否都有,否则下架

                if ($auction['start_status'] == 'time') {
                    // if($auction['end_time']<=time()){
                    //更新状态
                    $data['status'] = '3'; //状态:1=待展示,2=预展示,3=进行中,5=结束,6=已下架
                    Db::name('auction')->where('status', '=', '2')->where('id', '=', $auction['id'])->update($data);

                    $queue2 = \Imi\Queue\Facade\Queue::getQueue('auction');
                    $message2 = new \Imi\Queue\Model\Message;
                    //发布对列
                    $message2->setMessage(json_encode(['type' => 'auction_finish' . $auction['type_status'], 'auction' => $auction], JSON_UNESCAPED_UNICODE));
                    $queue2->push($message2, $auction['end_time'] - time());


                    //推送开始拍卖
                    //推送到拍卖群组里面信息
                    $auction_start_data = ['auction_id' => $auction['id'], 'end_date' => date('Y-m-d H:i:s', $auction['end_time']), 'type_status' => $auction['type_status']];
                    $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    $ToGroup_data = $tpmecms->ws_success('开始拍卖', $auction_start_data, 'auction_start');
                    \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
                    \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据
                }

                break;


            case 'auction_out1': //出局
                // $auction = $params['auction'];
                $id = $params['data']['id'];
                var_dump('出局' . $id);
                $where['id'] = ['<', $id];
                $where['auction_id'] = $auction['id'];
                $where['surety_status'] = '1'; //状态:1=保证金未,2=已退
                // $list = Db::name('auction_offer')->whereEx($where)->select()->getArray();


                // //推送开始拍卖
                // //推送到拍卖群组里面信息
                // $auction_start_data = ['auction_id' => $auction['id'], 'end_date' => date('Y-m-d H:i:s', $auction['end_time']), 'type_status' => $auction['type_status']];
                // $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                // $ToGroup_data = $tpmecms->ws_success('开始拍卖', $auction_start_data, 'auction_start');
                // \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
                // \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据


                // $tpmecm = new \ImiApp\ApiServer\Api\Controller\Tpmecms;

                // if ($list) {
                //     foreach ($list as $key => $val) {
                //修改状态 解冻保证金
                // var_dump($val['user_id'], $auction['buyer_surety'], $拍卖类型 . $auction['id'] . '出局', $拍卖类型, $id, 'surety');
                // $tpmecm->IncUserSurety($val['user_id'], $auction['buyer_surety'], $拍卖类型 . $auction['id'] . '出局', $拍卖类型, $id, 'surety');
                // $tpmecm->UnFreezeUser($val['user_id'], 'surety', 'auction1_surety',  $val['id'], $拍卖类型 . $auction['id'] . '出局');
                // Db::name('auction_offer')->where('id', '=', $val['id'])->update(['status' => '2', 'surety_status' => '2', 'surety_time' => time()]);
                //     }
                // }

                break; //出局 auction_out2 结束


            case 'auction_out2': //出局
                // $auction = $params['auction'];
                $id = $params['data']['id'];
                var_dump('出局' . $id);
                $where['id'] = ['<', $id];
                $where['auction_id'] = $auction['id'];
                $where['surety_status'] = '1'; //状态:1=保证金未,2=已退
                $list = Db::name('auction_offer')->whereEx($where)->select()->getArray();


                // //推送开始拍卖
                // //推送到拍卖群组里面信息
                // $auction_start_data = ['auction_id' => $auction['id'], 'end_date' => date('Y-m-d H:i:s', $auction['end_time']), 'type_status' => $auction['type_status']];
                // $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                // $ToGroup_data = $tpmecms->ws_success('开始拍卖', $auction_start_data, 'auction_start');
                // \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
                // \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据


                $tpmecm = new \ImiApp\ApiServer\Api\Controller\Tpmecms;

                if ($list) {
                    foreach ($list as $key => $val) {
                        //修改状态 解冻保证金
                        // var_dump($val['user_id'], $auction['buyer_surety'], $拍卖类型 . $auction['id'] . '出局', $拍卖类型, $id, 'surety');
                        $tpmecm->IncUserSurety($val['user_id'], $auction['buyer_surety'], $拍卖类型 . $auction['id'] . '出局', $拍卖类型, $id, 'surety');
                        $tpmecm->UnFreezeUser($val['user_id'], 'surety', 'auction2_surety',  $val['id'], $拍卖类型 . $auction['id'] . '出局');
                        Db::name('auction_offer')->where('id', '=', $val['id'])->update(['status' => '2', 'surety_status' => '2', 'surety_time' => time()]);
                    }
                }

                break; //出局 auction_out2 结束



            case 'auction_out3': //出局
                // $auction = $params['auction'];
                $id = $params['data']['id'];
                var_dump('出局' . $id);
                $where['id'] = ['<', $id];
                $where['auction_id'] = $auction['id'];
                $where['surety_status'] = '1'; //状态:1=保证金未,2=已退
                $list = Db::name('auction_offer')->whereEx($where)->select()->getArray();
                $tpmecm = new \ImiApp\ApiServer\Api\Controller\Tpmecms;

                if ($list) {
                    foreach ($list as $key => $val) {
                        //修改状态 解冻保证金
                        // var_dump($val['user_id'], $auction['buyer_surety'], $拍卖类型 . $auction['id'] . '出局', $拍卖类型, $id, 'surety');
                        $tpmecm->IncUserSurety($val['user_id'], $auction['buyer_surety'], $拍卖类型 . $auction['id'] . '出局', $拍卖类型, $id, 'surety');
                        $tpmecm->UnFreezeUser($val['user_id'], 'surety', 'auction3_surety',  $val['id'], $拍卖类型 . $auction['id'] . '出局');
                        Db::name('auction_offer')->where('id', '=', $val['id'])->update(['status' => '2', 'surety_status' => '2', 'surety_time' => time()]);
                    }
                }

                break; //出局 auction_out3 结束


            case 'auction_out4': //出局
                // $auction = $params['auction'];
                $id = $params['data']['id'];
                var_dump('出局' . $id);
                $where['auction_id'] = $auction['id'];
                $where['surety_status'] = '1'; //状态:1=保证金未,2=已退
                $前2名 = Db::name('auction_offer')->whereEx($where)->orderRaw('price asc')->page(1, 2)->column('id');
                $list = Db::name('auction_offer')->whereEx($where)->select()->getArray();
                $tpmecm = new \ImiApp\ApiServer\Api\Controller\Tpmecms;

                if ($list) {
                    foreach ($list as $key => $val) {
                        if (!in_array($val['id'], $前2名)) {
                            //修改状态 解冻保证金
                            // var_dump($val['user_id'], $auction['buyer_surety'], $拍卖类型 . $auction['id'] . '出局', $拍卖类型, $id, 'surety');
                            $tpmecm->IncUserSurety($val['user_id'], $auction['buyer_surety'], $拍卖类型 . $auction['id'] . '出局', $拍卖类型, $params['data']['id'], 'surety');
                            $tpmecm->UnFreezeUser($val['user_id'], 'surety', 'auction4_surety',  $val['id'], $拍卖类型 . $auction['id'] . '出局');
                            Db::name('auction_offer')->where('id', '=', $val['id'])->update(['status' => '2', 'surety_status' => '2', 'surety_time' => time()]);
                        }
                    }
                }


                break; //出局 auction_out4 结束



            case 'auction_out5': //出局

                // 暗拍升 应该一直冻结,直到结束 所以没有出局一说 期间第一次冻结保证金

                $where['auction_id'] = $auction['id'];
                $where['surety_status'] = '1'; //状态:1=保证金未,2=已退
                $前2名 = Db::name('auction_offer')->whereEx($where)->orderRaw('price desc')->page(1, 2)->column('id');
                $list = Db::name('auction_offer')->whereEx($where)->select()->getArray();
                $tpmecm = new \ImiApp\ApiServer\Api\Controller\Tpmecms;

                if ($list) {
                    foreach ($list as $key => $val) {
                        if (!in_array($val['id'], $前2名)) {
                            //修改状态 解冻保证金
                            // var_dump($val['user_id'], $auction['buyer_surety'], $拍卖类型 . $auction['id'] . '出局', $拍卖类型, $id, 'surety');
                            $tpmecm->IncUserSurety($val['user_id'], $auction['buyer_surety'], $拍卖类型 . $auction['id'] . '出局', $拍卖类型, $params['data']['id'], 'surety');
                            $tpmecm->UnFreezeUser($val['user_id'], 'surety', 'auction5_surety',  $val['id'], $拍卖类型 . $auction['id'] . '出局');
                            Db::name('auction_offer')->where('id', '=', $val['id'])->update(['status' => '2', 'surety_status' => '2', 'surety_time' => time()]);
                        }
                    }
                }

                break; //出局 auction_out5 结束






            case 'auction_timer_dec_price2': //定时降价auction_timer_dec_price2
                // if ($auction['auto_now_price'] > 1 && $auction['user_id'] < 1) {
                if ($auction['status'] < 5 && $auction['cut_status'] != '5' && ($auction['end_time'] > 0 && $auction['end_time'] > time())) {
                    //没有结束 并且时间还没到 
                    $arr_rand = [$auction['dec_price1'], $auction['dec_price2'], $auction['dec_price3'], $auction['dec_price4']];
                    $随机比例 = $arr_rand[array_rand($arr_rand)];

                    $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    $随机比例 = $tpmecms->ChangeRatio($随机比例);

                    $auto_now_price = round($auction['auto_now_price'] * (1 - $随机比例));

                    var_dump('随机比例' . $随机比例 . '$auction[auto_now_price]' . $auction['auto_now_price'] . '计算后价格' . $auto_now_price);

                    $now_price = $auto_now_price;

                    //截拍:1=待开始,2=自动降价,3=最后三分钟,5=结束',
                    if ($auto_now_price <= 1 || $auto_now_price == $auction['auto_now_price'] || $auction['cut_status'] == '3') { //$auction['minimum_price']
                        var_dump($auto_now_price . '对列低于1元 或者降价不动  开始最后3分钟' . $auction['id']);
                        //低于保留价
                        // $now_price = $auto_now_price = $auction['minimum_price'];
                        $end_time = time() + (int)config('auction_final_time');
                        $version = $auction['version'] + rand(1, 10);

                        var_dump('338  cut_status=>3');
                        Db::name('auction')->where('id', "=", $auction['id'])->update([
                            // 'auto_now_price' => $auto_now_price,
                            //  'now_price' => $now_price,
                            'updatetime' => time(),
                            'version' => $version,
                            'end_time' => $end_time,
                            'cut_status' => '3'
                        ]);
                        //截拍:1=待开始,2=自动降价,3=最后三分钟,5=结束

                        //活动结束
                        $queue2 = \Imi\Queue\Facade\Queue::getQueue('auction');
                        $message2 = new \Imi\Queue\Model\Message;


                        //发布对列
                        $message2->setMessage(json_encode(['type' => 'auction_finish' . $auction['type_status'], 'auction' => $auction, 'end_time' => $end_time, 'from' => '截拍或价格小于1,开始升价'], JSON_UNESCAPED_UNICODE));
                        $queue2->push($message2, (float)config('auction_final_time'));


                        //推送到拍卖群组里面信息
                        $auction_dec_price_inc = ['auction_id' => $auction['id'], 'end_date' => date('Y-m-d H:i:s', $end_time), 'now_price' => $now_price, 'type_status' => $auction['type_status'], 'version' => $version];
                        $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                        $ToGroup_data = $tpmecms->ws_success('降价拍卖升价拍开始', $auction_dec_price_inc, 'auction_dec_price_inc');
                        \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
                        \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据

                    } else {
                        $version = $auction['version'] + rand(1, 10);
                        Db::name('auction')->where('id', "=", $auction['id'])->update(['auto_now_price' => $auto_now_price, 'now_price' => $now_price, 'updatetime' => time(), 'version' => $version]);
                        $queue2 = \Imi\Queue\Facade\Queue::getQueue('auction');
                        $message2 = new \Imi\Queue\Model\Message;
                        //发布对列
                        $message2->setMessage(json_encode(['type' => 'auction_timer_dec_price2', 'auction' => $auction], JSON_UNESCAPED_UNICODE));
                        $queue2->push($message2, $auction['timer_dec_s']);


                        //推送到拍卖群组里面信息
                        $auction_dec_price = ['auction_id' => $auction['id'], 'end_date' => date('Y-m-d H:i:s', $auction['end_time']), 'now_price' => $now_price, 'type_status' => $auction['type_status'], 'version' => $version];
                        $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                        $ToGroup_data = $tpmecms->ws_success('降价拍卖降价', $auction_dec_price, 'auction_dec_price');
                        \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
                        \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据

                    }
                } else {
                    var_dump('请结束拍卖活动' . $auction['id'] . '价格已经降到' . $auction['auto_now_price']);

                    //活动结束
                    $queue2 = \Imi\Queue\Facade\Queue::getQueue('auction');
                    $message2 = new \Imi\Queue\Model\Message;

                    Db::name('auction')->where('id', '=', $auction['id'])->update(['end_time' => time()]);
                    //发布对列
                    $message2->setMessage(json_encode(['type' => 'auction_finish' . $auction['type_status'], 'auction' => $auction], JSON_UNESCAPED_UNICODE));
                    $queue2->push($message2, (float)config('auction_final_time'));
                }

                break;

            case 'auction_finish1': //结束auction_finish1
                if (isset($params['end_time'])) {
                    if ($auction['end_time'] > $params['end_time']) {

                        \Imi\Log\Log::error('结束时间不一致' . $auction['id'] . '正式结束时间' . date('Y-m-d H:i:s', $auction['end_time']) . '参数结束时间' . date('Y-m-d H:i:s', $params['end_time']));
                        break;
                    }
                }
                if (isset($params['from'])) {
                    \Imi\Log\Log::info($拍卖类型 . $auction['id'] . '来自' . $params['from']);
                }

                $拍中 = false;
                //判断是否成功
                // if ($auction['start_status'] == 'time') {

                if ($auction['end_time'] <= time() || $auction['max_price_status'] == '2') {
                    if ($auction['max_price_status'] == '2') {
                        var_dump($auction['id'] . '到达限高价格');
                    }

                    //判断用户是否在出价的最低价
                    $first = $this->getFirstOffer($auction['id']);
                    if ($first && $first['price'] >= $auction['minimum_price'] || $auction['max_price_status'] == '2') {
                        $status = '5'; //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍

                        if ($auction['how_cycle'] < $auction['cycle']) {
                            $this->AutoNextCycle($auction['id']); //进入下一个周期
                        }
                        $拍中 = true;
                    } else {
                        $status = '7'; //流拍
                        //需要扣卖家的
                        $this->扣除卖家流拍服务费($auction, $拍卖类型);

                        //超过流拍最大数 下架
                        if ($auction['pass_in_num'] >= config('pass_in_max_num')) {
                            //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍
                            Db::name('auction')->where('id', '=', $auction['id'])->update(['status' => '6', 'updatetime' => time()]);
                        } else {
                            $this->passIn($auction['id']);
                        }



                        //退用户保证金
                        $ps = $拍卖类型 . $auction['id'] . '未成功';
                        $tpmecm = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                        // $auction_offer = Db::name('auction_offer')->where('auction_id', '=', $auction['id'])->where('status', '=', '1')->orderRaw('id desc')->find();
                        // if ($auction_offer)
                        //     $tpmecm->UserSuretyReturn($auction_offer, $auction, $ps, $拍卖类型); //用户退保证金


                        //退用户积分积分
                        $tpmecm->ReturnUserAuctionEnrollScore($auction['id']);

                        //加库存
                        // $goods_sku_price_arr = explode(',', $auction['goods_sku_price_ids']);
                        // //
                        // foreach ($goods_sku_price_arr as $key => $sku_id) {
                        //     $this->IncGoodsStock($sku_id, 1, '流拍' . $拍卖类型 . '活动退回库存', $auction['id']);
                        // }
                        $tpmecm->解冻拍卖全部库存($auction, '流拍' . $拍卖类型 . '活动退回库存');
                    }
                    // }//判断 升价拍 时间 结束


                    // if ($auction['start_status'] == 'number') {
                    //     //判断保留价

                    //     // //判断用户是否在出价的最低价
                    //     $first = $this->getFirstOffer($auction['id']);
                    //     if ($first && $first['price'] >= $auction['minimum_price']) {
                    //         $status = '5'; //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍


                    //     } else {
                    //         $status = '7'; //流拍
                    //         //需要扣卖家的


                    //         //退用户保证金
                    //         $ps = $拍卖类型 . $auction['id'] . '未成功';
                    //         $tpmecm = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    //         $auction_offer = Db::name('auction_offer')->where('auction_id','=',$auction['id'])->where('status','=','1')->orderRaw('id desc')->find();
                    //         if($auction_offer)
                    //         $tpmecm->UserSuretyReturn($auction_offer,$auction,$ps,$拍卖类型);//用户退保证金


                    //         //退用户积分积分
                    //         $tpmecm->ReturnUserAuctionEnrollScore($auction['id']);

                    //     }
                    // }//判断 升价拍 人数 结束



                    Db::name('auction')->where('id', '=', $auction['id'])->update(['status' => $status]);
                    // 修改结束 

                    if ($status == '5') {
                        //给出价的人返积分
                        $list = Db::name('auction_offer')
                            ->where('auction_id', '=', $auction['id'])
                            ->where('status', '=', '2') //状态:1=领先,2=出局
                            ->where('shell_status', '=', '1') //状态:1=未补贴,2=已补贴
                            ->select()
                            ->getArray();
                        foreach ($list as $key => $val) {
                            if ($val['enroll_status'] == '2') {
                                $score = $auction['enroll_out_perk_score'];
                                $ps = $拍卖类型 . '报名,出局,补贴积分';
                            }
                            $tpmecm = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                            $tpmecm->IncUserScore($val['user_id'], $score, $ps, 22, $val['id']);
                            Db::name('auction_offer')->where('id', '=', $val['id'])->update(['shell_status' => '2']);
                        }
                    }

                    //生成一个该用户的订单
                    if ($拍中) {
                        $auction = Db::name('auction')->where('id', '=', $auction['id'])->find();
                        $this->createOrder($auction['id'], $auction['user_id'], $first['price']);
                    }





                    //推送到拍卖群组里面信息
                    $推送结束信息 = ['end_date' => date('Y-m-d H:i:s', $auction['end_time']), 'auction_id' => $auction['id'], 'type_status' => $auction['type_status']];
                    $tpmecm = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    $ToGroup_data = $tpmecm->ws_success('拍卖结束', $推送结束信息, 'auction_finish');
                    \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
                    \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据
                }

                // $id = $params['data']['id'];
                // $where['id'] = ['<',$id];
                // $where['auction_id'] = $auction['id'];
                // $where['status'] = '1';//状态:1=领先,2=出局
                // $list = Db::name('auction_offer')->whereEx($where)->select();
                // $tpmecm = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                // if($list){
                //     foreach ($list as $key => $val) {
                //         //修改状态 解冻保证金
                //         $tpmecm->IncUserSurety($val['user_id'], $auction['buyer_surety'], $拍卖类型.$auction['id'].'出局' , $拍卖类型, $id,'surety');
                //         Db::name('auction_offer')->where('id','=',$val['id'])->update(['status'=>'2','surety_status'=>'2','surety_time'=>time()]);
                //     }
                // }

                break; ////结束auction_finish1




                break; //定时降价auction_timer_dec_price2 结束

            case 'auction_finish2': //结束auction_finish2
                if (isset($params['end_time'])) {
                    if ($auction['end_time'] > $params['end_time']) {

                        \Imi\Log\Log::error('结束时间不一致' . $auction['id'] . '正式结束时间' . date('Y-m-d H:i:s', $auction['end_time']) . '参数结束时间' . date('Y-m-d H:i:s', $params['end_time']));
                        break;
                    }
                }
                if (isset($params['from'])) {
                    \Imi\Log\Log::info($拍卖类型 . $auction['id'] . '来自' . $params['from']);
                }

                $拍中 = false;
                //判断是否成功
                // if ($auction['start_status'] == 'time') {

                if ($auction['end_time'] <= time()) {



                    //判断用户是否在出价的最低价
                    $first = $this->getFirstOffer($auction['id']);
                    if ($first && $first['price'] >= $auction['minimum_price']) {
                        $status = '5'; //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍

                        var_dump('降价拍结束完成' . $auction['id']);
                        if ($auction['how_cycle'] < $auction['cycle']) {
                            $this->AutoNextCycle($auction['id']); //进入下一个周期
                        }
                        $拍中 = true;
                    } else {
                        $status = '7'; //流拍
                        //需要扣卖家的
                        $this->扣除卖家流拍服务费($auction, $拍卖类型);

                        //超过流拍最大数 下架
                        if ($auction['pass_in_num'] >= config('pass_in_max_num')) {
                            //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍
                            Db::name('auction')->where('id', '=', $auction['id'])->update(['status' => '6', 'updatetime' => time()]);
                        } else {
                            $this->passIn($auction['id']);
                        }



                        //退用户保证金
                        $ps = $拍卖类型 . $auction['id'] . '未成功';
                        $tpmecm = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                        $auction_offer = Db::name('auction_offer')->where('auction_id', '=', $auction['id'])->where('status', '=', '1')->orderRaw('id desc')->find();
                        if ($auction_offer)
                            $tpmecm->UserSuretyReturn($auction_offer, $auction, $ps, $拍卖类型); //用户退保证金


                        //退用户贝壳
                        $tpmecm->ReturnUserAuctionEnrollShell($auction['id']);

                        //加库存
                        // $goods_sku_price_arr = explode(',', $auction['goods_sku_price_ids']);
                        // //
                        // foreach ($goods_sku_price_arr as $key => $sku_id) {
                        //     $this->IncGoodsStock($sku_id, 1, '流拍' . $拍卖类型 . '活动退回库存', $auction['id']);
                        // }
                        $tpmecm->解冻拍卖全部库存($auction, '流拍' . $拍卖类型 . '活动退回库存');
                    }
                    // }//判断 升价拍 时间 结束


                    // if ($auction['start_status'] == 'number') {
                    //     //判断保留价

                    //     // //判断用户是否在出价的最低价
                    //     $first = $this->getFirstOffer($auction['id']);
                    //     if ($first && $first['price'] >= $auction['minimum_price']) {
                    //         $status = '5'; //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍


                    //     } else {
                    //         $status = '7'; //流拍
                    //         //需要扣卖家的


                    //         //退用户保证金
                    //         $ps = $拍卖类型 . $auction['id'] . '未成功';
                    //         $tpmecm = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    //         $auction_offer = Db::name('auction_offer')->where('auction_id','=',$auction['id'])->where('status','=','1')->orderRaw('id desc')->find();
                    //         if($auction_offer)
                    //         $tpmecm->UserSuretyReturn($auction_offer,$auction,$ps,$拍卖类型);//用户退保证金


                    //         //退用户积分积分
                    //         $tpmecm->ReturnUserAuctionEnrollScore($auction['id']);

                    //     }
                    // }//判断 升价拍 人数 结束


                    $update_data['status'] = $status;
                    Db::name('auction')->where('id', '=', $auction['id'])->update($update_data);
                    // 修改结束 

                    if ($status == '5') {
                        //给出价的人返贝壳
                        //如果是截拍 报名费返给截拍到人

                        // 7-22


                        // $list = Db::name('auction_offer')->where('auction_id', '=', $auction['id'])->where('shell_status', '=', '1')->select()->getArray();
                        // foreach ($list as $key => $val) {
                        //     if ($val['enroll_status'] == '1') {
                        //         $shell = $auction['out_perk_shell'];
                        //         $ps = $拍卖类型 . '未报名,出局,补贴贝壳';
                        //     } else {
                        //         $shell = $auction['enroll_out_perk_shell'];
                        //         $ps = $拍卖类型 . '报名,出局,补贴贝壳';
                        //     }
                        //     $tpmecm = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                        //     $tpmecm->IncUserShell($val['user_id'], $shell, $ps, 22, $val['id']);
                        //     Db::name('auction_offer')->where('id', '=', $val['id'])->update(['shell_status' => '2']);
                        // }
                    }

                    //生成一个该用户的订单
                    if ($拍中) {
                        $auction = Db::name('auction')->where('id', '=', $auction['id'])->find();
                        $this->createOrder($auction['id'], $auction['user_id'], $first['price']);
                    }




                    //推送到拍卖群组里面信息
                    $推送结束信息 = ['end_date' => date('Y-m-d H:i:s', $auction['end_time']), 'auction_id' => $auction['id'], 'type_status' => $auction['type_status']];
                    $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    $ToGroup_data = $tpmecms->ws_success('拍卖结束', $推送结束信息, 'auction_finish');
                    \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
                    \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据
                }

                // $id = $params['data']['id'];
                // $where['id'] = ['<',$id];
                // $where['auction_id'] = $auction['id'];
                // $where['status'] = '1';//状态:1=领先,2=出局
                // $list = Db::name('auction_offer')->whereEx($where)->select();
                // $tpmecm = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                // if($list){
                //     foreach ($list as $key => $val) {
                //         //修改状态 解冻保证金
                //         $tpmecm->IncUserSurety($val['user_id'], $auction['buyer_surety'], $拍卖类型.$auction['id'].'出局' , $拍卖类型, $id,'surety');
                //         Db::name('auction_offer')->where('id','=',$val['id'])->update(['status'=>'2','surety_status'=>'2','surety_time'=>time()]);
                //     }
                // }

                break; ////结束auction_finish2


            case 'auction_finish3': //结束auction_finish3

                if (isset($params['end_time'])) {
                    if ($auction['end_time'] > $params['end_time']) {

                        \Imi\Log\Log::error('结束时间不一致' . $auction['id'] . '正式结束时间' . date('Y-m-d H:i:s', $auction['end_time']) . '参数结束时间' . date('Y-m-d H:i:s', $params['end_time']));
                        break;
                    }
                }
                if (isset($params['from'])) {
                    \Imi\Log\Log::info($拍卖类型 . $auction['id'] . '来自' . $params['from']);
                }

                //判断是否成功
                $拍中 = false;
                // if ($auction['start_status'] == 'time') {

                if ($auction['end_time'] <= time() || $auction['max_price_status'] == '2') {
                    if ($auction['max_price_status'] == '2') {
                        var_dump($auction['id'] . '到达限高价格');
                    }


                    //判断用户是否在出价的最低价
                    $first = $this->getFirstOffer($auction['id']);

                    $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;

                    $出价总补贴贝壳 = $tpmecms->获取拍卖出局总补贴贝壳($auction, $first);
                    if ($first && $first['price'] >= $auction['minimum_price'] || $auction['max_price_status'] == '2') {
                        $status = '5'; //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍

                        if ($auction['how_cycle'] < $auction['cycle']) {
                            $this->AutoNextCycle($auction['id']); //进入下一个周期
                        }
                        $拍中 = true;
                    } else {
                        $status = '7'; //流拍
                        //需要扣卖家的
                        $this->扣除卖家流拍服务费($auction, $拍卖类型);

                        //超过流拍最大数 下架
                        if ($auction['pass_in_num'] >= config('pass_in_max_num')) {
                            //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍
                            Db::name('auction')->where('id', '=', $auction['id'])->update(['status' => '6', 'updatetime' => time()]);
                        } else {
                            $this->passIn($auction['id']);
                        }



                        //退用户保证金
                        $ps = $拍卖类型 . $auction['id'] . '未成功';
                        $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                        $auction_offer = Db::name('auction_offer')->where('auction_id', '=', $auction['id'])->where('status', '=', '1')->orderRaw('id desc')->find();
                        if ($auction_offer)
                            $tpmecms->UserSuretyReturn($auction_offer, $auction, $ps, $拍卖类型); //用户退保证金


                        //退用户积分积分
                        $tpmecms->ReturnUserAuctionEnrollScore($auction['id']);

                        //加库存
                        // $goods_sku_price_arr = explode(',', $auction['goods_sku_price_ids']);
                        // //
                        // foreach ($goods_sku_price_arr as $key => $sku_id) {
                        //     $this->IncGoodsStock($sku_id, 1, '流拍' . $拍卖类型 . '活动退回库存', $auction['id']);
                        // }
                        $tpmecms->解冻拍卖全部库存($auction, '流拍' . $拍卖类型 . '活动退回库存');
                    }
                    // }//判断 升价拍 时间 结束


                    // if ($auction['start_status'] == 'number') {
                    //     //判断保留价

                    //     // //判断用户是否在出价的最低价
                    //     $first = $this->getFirstOffer($auction['id']);
                    //     if ($first && $first['price'] >= $auction['minimum_price']) {
                    //         $status = '5'; //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍


                    //     } else {
                    //         $status = '7'; //流拍
                    //         //需要扣卖家的


                    //         //退用户保证金
                    //         $ps = $拍卖类型 . $auction['id'] . '未成功';
                    //         $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    //         $auction_offer = Db::name('auction_offer')->where('auction_id','=',$auction['id'])->where('status','=','1')->orderRaw('id desc')->find();
                    //         if($auction_offer)
                    //         $tpmecms->UserSuretyReturn($auction_offer,$auction,$ps,$拍卖类型);//用户退保证金


                    //         //退用户积分积分
                    //         $tpmecms->ReturnUserAuctionEnrollScore($auction['id']);

                    //     }
                    // }//判断 升价拍 人数 结束



                    Db::name('auction')->where('id', '=', $auction['id'])->update(['status' => $status]);
                    // 修改结束 

                    if ($status == '5') {

                        //给出价的人返贝壳
                        $list = Db::name('auction_offer')->where('auction_id', '=', $auction['id'])->where('status', '=', '2')->where('shell_status', '=', '1')->select()->getArray();
                        foreach ($list as $key => $val) {
                            if ($val['enroll_status'] == '1' && $val['score'] > 0) {
                                $shell = $auction['out_perk_shell'];
                                $ps = $拍卖类型 . '未报名,出局,补贴贝壳';
                            } else {
                                $shell = $auction['enroll_out_perk_shell'];
                                $ps = $拍卖类型 . '报名,出局,补贴贝壳';
                            }
                            $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                            $tpmecms->IncUserShell($val['user_id'], $shell, $ps, 22, $val['id']);
                            Db::name('auction_offer')->where('id', '=', $val['id'])->update(['shell_status' => '2']);
                        }
                    }

                    //生成一个该用户的订单
                    if ($拍中) {
                        $auction = Db::name('auction')->where('id', '=', $auction['id'])->find();
                        $this->createOrder($auction['id'], $auction['user_id'], $first['price']);
                    }




                    //推送到拍卖群组里面信息
                    $推送结束信息 = ['end_date' => date('Y-m-d H:i:s', $auction['end_time']), 'auction_id' => $auction['id'], 'type_status' => $auction['type_status']];
                    $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    $ToGroup_data = $tpmecms->ws_success('拍卖结束', $推送结束信息, 'auction_finish');
                    \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
                    \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据
                }

                // $id = $params['data']['id'];
                // $where['id'] = ['<',$id];
                // $where['auction_id'] = $auction['id'];
                // $where['status'] = '1';//状态:1=领先,2=出局
                // $list = Db::name('auction_offer')->whereEx($where)->select();
                // $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                // if($list){
                //     foreach ($list as $key => $val) {
                //         //修改状态 解冻保证金
                //         $tpmecms->IncUserSurety($val['user_id'], $auction['buyer_surety'], $拍卖类型.$auction['id'].'出局' , $拍卖类型, $id,'surety');
                //         Db::name('auction_offer')->where('id','=',$val['id'])->update(['status'=>'2','surety_status'=>'2','surety_time'=>time()]);
                //     }
                // }

                break; ////结束auction_finish3



            case 'auction_finish4': //结束auction_finish4
                if (isset($params['end_time'])) {
                    if ($auction['end_time'] > $params['end_time']) {

                        \Imi\Log\Log::error('结束时间不一致' . $auction['id'] . '正式结束时间' . date('Y-m-d H:i:s', $auction['end_time']) . '参数结束时间' . date('Y-m-d H:i:s', $params['end_time']));
                        break;
                    }
                }
                if (isset($params['from'])) {
                    \Imi\Log\Log::info($拍卖类型 . $auction['id'] . '来自' . $params['from']);
                }

                $拍中 = false;
                //判断是否成功
                // if ($auction['start_status'] == 'time') {

                if ($auction['end_time'] <= time()) {



                    //判断用户是否在出价的最低价
                    $first = $this->getMinOffer($auction['id']);

                    $second = $this->getMinSecondOffer($auction['id']);
                    if ($first && $second['price'] >= $auction['minimum_price']) {
                        $status = '5'; //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍

                        if ($auction['how_cycle'] < $auction['cycle']) {
                            $this->AutoNextCycle($auction['id']); //进入下一个周期
                        }
                        $拍中 = true;
                        //生成一个该用户的订单

                    } else {
                        $status = '7'; //流拍
                        //需要扣卖家的
                        $this->扣除卖家流拍服务费($auction, $拍卖类型);

                        //超过流拍最大数 下架
                        if ($auction['pass_in_num'] >= config('pass_in_max_num')) {
                            //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍
                            Db::name('auction')->where('id', '=', $auction['id'])->update(['status' => '6', 'updatetime' => time()]);
                        } else {
                            $this->passIn($auction['id']);
                        }



                        //退用户保证金
                        $ps = $拍卖类型 . $auction['id'] . '未成功';
                        $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                        $auction_offer = Db::name('auction_offer')->where('auction_id', '=', $auction['id'])->where('status', '=', '1')->orderRaw('id desc')->find();
                        if ($auction_offer)
                            $tpmecms->UserSuretyReturn($auction_offer, $auction, $ps, $拍卖类型); //用户退保证金


                        //退用户积分
                        $tpmecms->ReturnUserAuctionEnrollScore($auction['id']);

                        //加库存
                        // $goods_sku_price_arr = explode(',', $auction['goods_sku_price_ids']);
                        // //
                        // foreach ($goods_sku_price_arr as $key => $sku_id) {
                        //     $this->IncGoodsStock($sku_id, 1, '流拍' . $拍卖类型 . '活动退回库存', $auction['id']);
                        // }
                        $tpmecms->解冻拍卖全部库存($auction, '流拍' . $拍卖类型 . '活动退回库存');
                    }
                    // }//判断 升价拍 时间 结束


                    // if ($auction['start_status'] == 'number') {
                    //     //判断保留价

                    //     // //判断用户是否在出价的最低价
                    //     $first = $this->getFirstOffer($auction['id']);
                    //     if ($first && $first['price'] >= $auction['minimum_price']) {
                    //         $status = '5'; //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍


                    //     } else {
                    //         $status = '7'; //流拍
                    //         //需要扣卖家的


                    //         //退用户保证金
                    //         $ps = $拍卖类型 . $auction['id'] . '未成功';
                    //         $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    //         $auction_offer = Db::name('auction_offer')->where('auction_id','=',$auction['id'])->where('status','=','1')->orderRaw('id desc')->find();
                    //         if($auction_offer)
                    //         $tpmecms->UserSuretyReturn($auction_offer,$auction,$ps,$拍卖类型);//用户退保证金


                    //         //退用户积分积分
                    //         $tpmecms->ReturnUserAuctionEnrollScore($auction['id']);

                    //     }
                    // }//判断 升价拍 人数 结束


                    $update_data['status'] = $status;
                    Db::name('auction')->where('id', '=', $auction['id'])->update($update_data);
                    // 修改结束 

                    if ($status == '5') {
                        //给出价的人返贝壳
                        //如果是截拍 报名费返给截拍到人

                        // 7-22

                        // $list = Db::name('auction_offer')->where('auction_id', '=', $auction['id'])->where('shell_status', '=', '1')->select()->getArray();
                        // foreach ($list as $key => $val) {
                        //     if ($val['enroll_status'] == '1') {
                        //         $shell = $auction['out_perk_shell'];
                        //         $ps = $拍卖类型 . '未报名,出局,补贴贝壳';
                        //     } else {
                        //         $shell = $auction['enroll_out_perk_shell'];
                        //         $ps = $拍卖类型 . '报名,出局,补贴贝壳';
                        //     }
                        //     $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                        //     $tpmecms->IncUserShell($val['user_id'], $shell, $ps, 22, $val['id']);
                        //     Db::name('auction_offer')->where('id', '=', $val['id'])->update(['shell_status' => '2']);
                        // }
                    }

                    //生成一个该用户的订单
                    if ($拍中) {
                        $auction = Db::name('auction')->where('id', '=', $auction['id'])->find();
                        $this->createOrder($auction['id'], $auction['user_id'], $second['price']);
                    }




                    //推送到拍卖群组里面信息
                    $推送结束信息 = ['end_date' => date('Y-m-d H:i:s', $auction['end_time']), 'auction_id' => $auction['id'], 'type_status' => $auction['type_status']];
                    $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    $ToGroup_data = $tpmecms->ws_success('拍卖结束', $推送结束信息, 'auction_finish');
                    \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
                    \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据
                }

                // $id = $params['data']['id'];
                // $where['id'] = ['<',$id];
                // $where['auction_id'] = $auction['id'];
                // $where['status'] = '1';//状态:1=领先,2=出局
                // $list = Db::name('auction_offer')->whereEx($where)->select();
                // $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                // if($list){
                //     foreach ($list as $key => $val) {
                //         //修改状态 解冻保证金
                //         $tpmecms->IncUserSurety($val['user_id'], $auction['buyer_surety'], $拍卖类型.$auction['id'].'出局' , $拍卖类型, $id,'surety');
                //         Db::name('auction_offer')->where('id','=',$val['id'])->update(['status'=>'2','surety_status'=>'2','surety_time'=>time()]);
                //     }
                // }

                break; ////结束auction_finish4


            case 'auction_finish5': //结束auction_finish5
                if (isset($params['end_time'])) {
                    if ($auction['end_time'] > $params['end_time']) {

                        \Imi\Log\Log::error('结束时间不一致' . $auction['id'] . '正式结束时间' . date('Y-m-d H:i:s', $auction['end_time']) . '参数结束时间' . date('Y-m-d H:i:s', $params['end_time']));
                        break;
                    }
                }
                if (isset($params['from'])) {
                    \Imi\Log\Log::info($拍卖类型 . $auction['id'] . '来自' . $params['from']);
                }

                $拍中 = false;
                //判断是否成功
                // if ($auction['start_status'] == 'time') {

                if ($auction['end_time'] <= time() || $auction['max_price_status'] == '2') {
                    if ($auction['max_price_status'] == '2') {
                        var_dump($auction['id'] . '到达限高价格');
                    }

                    //判断用户是否在出价的最低价
                    $second = $this->getSecondOffer($auction['id']);
                    if ($second && $second['price'] >= $auction['minimum_price']) { //|| $auction['max_price_status'] == '2'
                        $status = '5'; //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍

                        if ($auction['how_cycle'] < $auction['cycle']) {
                            $this->AutoNextCycle($auction['id']); //进入下一个周期
                        }

                        $拍中 = true;
                        //生成一个该用户的订单

                    } else {

                        if ($auction['cut_status'] == '3') {
                            $status = '7'; //流拍
                            //需要扣卖家的
                            $this->扣除卖家流拍服务费($auction, $拍卖类型);



                            //超过流拍最大数 下架
                            if ($auction['pass_in_num'] >= config('pass_in_max_num')) {
                                //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍
                                Db::name('auction')->where('id', '=', $auction['id'])->update(['status' => '6', 'updatetime' => time()]);
                            } else {
                                $this->passIn($auction['id']);
                            }



                            //退用户保证金
                            $ps = $拍卖类型 . $auction['id'] . '未成功';
                            $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                            $auction_offer = Db::name('auction_offer')->where('auction_id', '=', $auction['id'])->where('status', '=', '1')->orderRaw('id desc')->find();
                            if ($auction_offer)
                                $tpmecms->UserSuretyReturn($auction_offer, $auction, $ps, $拍卖类型); //用户退保证金


                            //退用户积分积分
                            $tpmecms->ReturnUserAuctionEnrollScore($auction['id']);

                            //加库存
                            // $goods_sku_price_arr = explode(',', $auction['goods_sku_price_ids']);
                            // //
                            // foreach ($goods_sku_price_arr as $key => $sku_id) {
                            //     $this->IncGoodsStock($sku_id, 1, '流拍' . $拍卖类型 . '活动退回库存', $auction['id']);
                            // }
                            $tpmecms->解冻拍卖全部库存($auction, '流拍' . $拍卖类型 . '活动退回库存');

                            var_dump('有执行到873 拍卖流拍');
                        } else {
                            //延时拍
                            $status = '3'; //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍

                            $end_time = time() + config('auction_final_time');
                            //重新计时
                            var_dump('876  cut_status=>3');
                            Db::name('auction')->where('id', '=', $auction['id'])->update(['end_time' => $end_time, 'cut_status' => '3']);
                            // 修改最终时间
                            //从现在加时间
                            //修改队列结束时间


                            //推送到拍卖群组里面信息
                            $auction_update_end_time_data = ['auction_final_time' => config('auction_final_time'), 'end_date' => date('Y-m-d H:i:s', $end_time), 'type_status' => $auction['type_status']];
                            $ToGroup_data = $tpmecms->ws_success('更新结束时间', $auction_update_end_time_data, 'auction_update_end_time');
                            \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
                            \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据


                            //活动结束
                            $queue2 = \Imi\Queue\Facade\Queue::getQueue('auction');
                            $message2 = new \Imi\Queue\Model\Message;
                            //发布对列
                            $message2->setMessage(json_encode(['type' => 'auction_finish' . $auction['type_status'], 'auction' => $auction], JSON_UNESCAPED_UNICODE));
                            $queue2->push($message2, (float)config('auction_final_time'));
                            break;
                        }
                    }
                    // }//判断 升价拍 时间 结束


                    // if ($auction['start_status'] == 'number') {
                    //     //判断保留价

                    //     // //判断用户是否在出价的最低价
                    //     $second = $this->getsecondOffer($auction['id']);
                    //     if ($second && $second['price'] >= $auction['minimum_price']) {
                    //         $status = '5'; //状态:1=待展示,2=预展示,3=进行中,5=完成结束,6=已下架,7=流拍


                    //     } else {
                    //         $status = '7'; //流拍
                    //         //需要扣卖家的


                    //         //退用户保证金
                    //         $ps = $拍卖类型 . $auction['id'] . '未成功';
                    //         $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    //         $auction_offer = Db::name('auction_offer')->where('auction_id','=',$auction['id'])->where('status','=','1')->orderRaw('id desc')->find();
                    //         if($auction_offer)
                    //         $tpmecms->UserSuretyReturn($auction_offer,$auction,$ps,$拍卖类型);//用户退保证金


                    //         //退用户积分积分
                    //         $tpmecms->ReturnUserAuctionEnrollScore($auction['id']);

                    //     }
                    // }//判断 升价拍 人数 结束



                    Db::name('auction')->where('id', '=', $auction['id'])->update(['status' => $status]);
                    // 修改结束 

                    if ($status == '5') {
                        //入场费和改价格费 全给中拍的人

                        $sum_score = (float)Db::name('auction_enroll')->where('auction_id', '=', $auction['id'])->sum('score');

                        $offer_score = (float)Db::name('auction_offer')->where('auction_id', '=', $auction['id'])->sum('score');

                        $sum_score = $offer_score + $sum_score;
                        var_dump('降价拍退截拍贝壳', $sum_score);
                        if ($sum_score > 0) {
                            $this->IncUserScore($auction['user_id'], $sum_score, $拍卖类型 . $auction['id'] . '结束', 29, $auction['id']);
                        } else {
                            var_dump('没有人报名和出价?' . $auction['id']);
                        }
                        $now_price =

                            Db::name('auction')->where('id', '=', $auction['id'])->update(['cut_status' => '5', 'status' => '5', 'now_price' => $second['price']]);


                        // $list = Db::name('auction_offer')->where('auction_id', '=', $auction['id'])->where('shell_status', '=', '1')->select()->getArray();




                    }


                    //生成一个该用户的订单
                    if ($拍中) {
                        $auction = Db::name('auction')->where('id', '=', $auction['id'])->find();
                        $this->createOrder($auction['id'], $auction['user_id'], $second['price']);
                    }




                    //推送到拍卖群组里面信息
                    $推送结束信息 = ['end_date' => date('Y-m-d H:i:s', $auction['end_time']), 'auction_id' => $auction['id'], 'type_status' => $auction['type_status']];
                    $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                    $ToGroup_data = $tpmecms->ws_success('拍卖结束', $推送结束信息, 'auction_finish');
                    \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
                    \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据
                    var_dump('有执行到972');
                }

                // $id = $params['data']['id'];
                // $where['id'] = ['<',$id];
                // $where['auction_id'] = $auction['id'];
                // $where['status'] = '1';//状态:1=领先,2=出局
                // $list = Db::name('auction_offer')->whereEx($where)->select();
                // $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                // if($list){
                //     foreach ($list as $key => $val) {
                //         //修改状态 解冻保证金
                //         $tpmecms->IncUserSurety($val['user_id'], $auction['buyer_surety'], $拍卖类型.$auction['id'].'出局' , $拍卖类型, $id,'surety');
                //         Db::name('auction_offer')->where('id','=',$val['id'])->update(['status'=>'2','surety_status'=>'2','surety_time'=>time()]);
                //     }
                // }

                break; ////结束auction_finish5


            default:
                # code...
                break;
        }



        // 将消息标记为成功
        $queue->success($message);
    }

    private function getFirstOffer($id)
    {
        return Db::name('auction_offer')->where('auction_id', '=', $id)->where('status', '=', '1')->orderRaw('id desc')->find();
    }



    private function getMinOffer($id)
    {
        return Db::name('auction_offer')->where('auction_id', '=', $id)->where('status', '=', '1')->orderRaw('price asc')->find();
    }

    private function getMinSecondOffer($id)
    {
        return Db::name('auction_offer')->where('auction_id', '=', $id)->where('status', '=', '1')->orderRaw('price asc,time asc')->page(2, 1)->find();
    }


    /**
     * 第二名
     *
     * @param int $id
     * @return void
     */
    private function getSecondOffer($id)
    {
        return Db::name('auction_offer')->where('auction_id', '=', $id)->orderRaw('price desc,time asc')->page(2, 1)->find();
    }

    /**
     * 获取拍卖活动
     *
     * @param array $val 
     * @return void
     */
    protected function getAuctionData($id)
    {
        $auction = Db::name('auction')->where('id', '=', $id)->find();
        return $auction;
    }


    /**
     * 扣除卖家流拍服务费
     *
     *
     * @param array $auction 拍卖数组
     * @param string $拍卖类型 拍卖类型
     * @throws conditon
     **/
    public function 扣除卖家流拍服务费($auction, $拍卖类型)
    {
        $offer = Db::name('auction_offer')->where('auction_id', '=', $auction['id'])->find();
        if ($offer) {
            //扣除卖家点流拍服务费费
            $pass_in_service_ratio = ((float)config('pass_in_service_ratio') / 100);
            $pass_in_service_fee = $pass_in_service_ratio * $auction['now_price'];
            if ($pass_in_service_fee < 1) {
                $pass_in_service_fee = 1;
            }
            $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
            $tpmecms->DecStoreMoney($auction['store_id'], $pass_in_service_fee, '流拍服务费' . $拍卖类型, 25, $auction['id']);
            //扣除卖家点流拍服务费费
        }
    }
    /**
     * 流拍
     *
     * @param int $id
     * @return void
     */
    private function passIn($id)
    {
        $auction = $this->getAuctionData($id);

        $res = Db::name('auction')->where('old_id', '=', $id)->find();
        if (!$res) {
            $new  = $auction;

            switch ($auction['type_status']) {
                case '1':
                    $拍卖类型 = '贝壳竞拍';
                    $new['now_price'] =  $auction['start_price']; //最新价格
                    break;
                case '2':
                    $拍卖类型 = '降价拍';
                    $new['now_price'] =  $auction['start_price']; //最新价格
                    $new['auto_now_price'] =  $auction['start_price']; //最新价格
                    break;
                case '3':
                    $拍卖类型 = '升价拍';
                    break;
                case '4':
                    $拍卖类型 = '暗拍(降价拍)';
                    break;
                case '5':
                    $拍卖类型 = '暗拍(升价拍)';
                    break;
                default:
                    $new['now_price'] =  0; //最新价格
                    break;
            }


            $new['version'] = 1;
            $new['likes'] = 0;
            $new['views'] = 0;
            unset($new['user_id']);
            unset($new['id']);
            unset($new['cut_status']);
            unset($new['stock_status']);

            if ($auction['start_status'] == 'time') {


                $new['start_time'] = time() + $auction['advance_show_s'];
                $new['end_time'] = $new['start_time'] + $auction['end_s'];
                $new['createtime'] = time();
                $new['updatetime'] = time();
                $new['status'] = '2';
                $new['pass_in_num'] = $auction['pass_in_num'] + 1;
                if ($auction['old_id'] > 0) {
                } else {
                    $new['old_id'] = $auction['id'];
                }

                unset($new['id']);
                $new['id'] = Db::name('auction')->insert($new)->getLastInsertId();
                $auction = $new;

                $queue = \Imi\Queue\Facade\Queue::getQueue('auction');
                $message = new \Imi\Queue\Model\Message;
                //发布对列
                $message->setMessage(json_encode(['type' => 'update_auction_status' . $auction['type_status'], 'auction' => $auction], JSON_UNESCAPED_UNICODE));
                $queue->push($message, (float)$auction['advance_show_s']);
            }


            if ($auction['start_status'] == 'number') {


                $new['start_time'] = '';
                $new['end_time'] = '';
                $new['status'] = '2';
                $new['pass_in_num'] = $auction['pass_in_num'] + 1;
                $new['createtime'] = time();
                $new['updatetime'] = time();

                if ($auction['old_id'] > 0) {
                } else {
                    $new['old_id'] = $auction['id'];
                }

                unset($new['id']);
                $new['id'] = Db::name('auction')->insert($new)->getLastInsertId();
                $auction = $new;

                // $queue = \Imi\Queue\Facade\Queue::getQueue('auction');
                // $message = new \Imi\Queue\Model\Message;
                // //发布对列
                // $message->setMessage(json_encode(['type' => 'auction_finish3' , 'auction' => $auction], JSON_UNESCAPED_UNICODE));
                // $queue->push($message, (float)$auction['end_s']);
            }
        }
    }


    /**
     * 自动下一期
     *
     * @param int $id
     * @return void
     */
    private function AutoNextCycle($id)
    {
        $auction = $this->getAuctionData($id);

        $new['how_cycle'] = $auction['how_cycle'] + 1; //第几个周期

        $res = Db::name('auction')->whereEx($new)->where('old_id', '=', $id)->count();
        if (!$res) {








            if ($auction['start_status'] == 'time') {
                $new  = $auction;
                $new['now_price'] =  0; //最新价格
                $new['start_time'] = time() + $auction['advance_show_s'];
                $new['end_time'] = $new['start_time'] + $auction['end_s'];
                $new['createtime'] = time();
                $new['updatetime'] = time();
                $new['status'] = '2';

                $new['how_cycle'] = $auction['how_cycle'] + 1; //第几个周期
                $new['auto_status'] = '2'; //自动状态:1=非自动,2=自动

                if ($auction['old_id'] > 0) {
                } else {
                    $new['old_id'] = $auction['id'];
                }

                // unset($new['id']);
                // $new['id'] = Db::name('auction')->insert($new)->getLastInsertId();
                // $auction = $new;
            }


            if ($auction['start_status'] == 'number') {
                $new  = $auction;
                $new['now_price'] =  0; //最新价格
                $new['start_time'] = '';
                $new['end_time'] = '';
                $new['status'] = '2';

                $new['how_cycle'] = $auction['how_cycle'] + 1; //第几个周期
                $new['auto_status'] = '2'; //自动状态:1=非自动,2=自动
                if ($auction['old_id'] > 0) {
                } else {
                    $new['old_id'] = $auction['id'];
                }
                $new['createtime'] = time();
                $new['updatetime'] = time();
                // unset($new['id']);



                // $queue = \Imi\Queue\Facade\Queue::getQueue('auction');
                // $message = new \Imi\Queue\Model\Message;
                // //发布对列
                // $message->setMessage(json_encode(['type' => 'auction_finish3' , 'auction' => $auction], JSON_UNESCAPED_UNICODE));
                // $queue->push($message, (float)$auction['end_s']);
            }


            switch ($auction['type_status']) {
                case '1':
                    $拍卖类型 = '贝壳竞拍';
                    $new['now_price'] =  $auction['start_price']; //最新价格
                    break;
                case '2':
                    $拍卖类型 = '降价拍';
                    $new['now_price'] =  $auction['start_price']; //最新价格
                    $new['auto_now_price'] =  $auction['start_price']; //最新价格
                    break;
                case '3':
                    $拍卖类型 = '升价拍';
                    break;
                case '4':
                    $拍卖类型 = '暗拍(降价拍)';
                    break;
                case '5':
                    $拍卖类型 = '暗拍(升价拍)';
                    break;
                default:
                    $new['now_price'] =  0; //最新价格
                    break;
            }

            //统一卸载

            unset($new['id']);
            unset($new['views']);
            unset($new['likes']);
            unset($new['user_id']);
            unset($new['pass_in_num']);
            // unset($new['views']);
            // unset($new['views']);
            $new['id'] = Db::name('auction')->insert($new)->getLastInsertId();
            $auction = $new;

            if ($auction['start_status'] == 'time') {

                $queue = \Imi\Queue\Facade\Queue::getQueue('auction');
                $message = new \Imi\Queue\Model\Message;
                //发布对列
                $message->setMessage(json_encode(['type' => 'update_auction_status' . $auction['type_status'], 'auction' => $auction], JSON_UNESCAPED_UNICODE));
                $queue->push($message, (float)$auction['advance_show_s']);
            }


            //推送到拍卖群组里面信息
            $推送信息 = ['auction_id' => $auction['id'], 'type_status' => $auction['type_status']];
            $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
            $ToGroup_data = $tpmecms->ws_success('拍卖显示', $推送信息, 'auction_show');
            \Imi\Server\Server::sendToGroup('auction_' . $auction['id'], $ToGroup_data, 'main');
            \Imi\Server\Server::sendToGroup(WS_拍卖列表, $ToGroup_data, 'main'); //给拍卖群列表发送数据
        }
    }





    /**
     * 创建拍中订单
     *
     * @param int $id 活动id
     * @param int $user_id 用户id
     * @param float $price 中拍价格
     * @return void
     */
    private function createAuctionSuccess($id, $user_id, $price = null)
    {







        //获取拍卖信息 (用户 商品 )
        $auction = Db::name('auction')->where('id', '=', $id)->find();

        $offer = Db::name('auction_offer')->where('auction_id', '=', $id)->where('user_id', '=', $user_id)->orderRaw('id desc')->find();
        $支付超时 = (int)config('unpay_order_cancel_time');
        switch ($auction['type_status']) {
            case '1':
                $拍卖类型 = '贝壳竞拍';
                break;
            case '2':
                $拍卖类型 = '降价拍';
                break;
            case '3':
                $拍卖类型 = '升价拍';
                break;
            case '4':
                $拍卖类型 = '暗拍(降价拍)';
                $支付超时 = (int)config('auction4_overtime_day') * 86400;
                break;
            case '5':
                $拍卖类型 = '暗拍(升价拍)';
                break;
            default:
                break;
        }


        $data['auction_id'] = $id;
        $data['user_id'] = $user_id;

        switch ($auction['type_status']) {
            case '1':
                $data['shell'] = $price;
                break;

            default:
                $data['money'] = $price;
                break;
        }

        $data['type_status'] = $auction['type_status'];

        $res = Db::name('auction_success')->whereEx($data)->find();
        if (!$res) {
            //没有才创建
            $data['start_time'] = time();
            $data['end_time'] = $data['start_time'] + $支付超时;
            $data['status'] = 'un'; //状态:un=待操作,post=寄拍,buy=购买,timeout=超时
            $data['dispatch_ids'] = $auction['dispatch_ids']; //运费模版
            $data['goods_sku_price_id'] =  $offer['goods_sku_price_id'];


            $auction_success_id =  Db::name('auction_success')->insert($data)->getLastInsertId();


            //定时器 多久后判断是否已经付款,未付款取消订单
            $timerId2 = \Imi\Timer\Timer::after($支付超时 * 1000, function () use ($auction_success_id) { //(int)config('unpay_order_cancel_time')

                if ($this->bug) var_dump('拍卖超时未支付,订单:auction_success_id' . $auction_success_id);
                $this->拍卖订单超时取消订单($auction_success_id);
            });
        } else {
            \Imi\Log\Log::error('多次创建拍卖订单' . $auction['id']);
        }
    }




    public function  拍卖订单超时取消订单($auction_success_id)
    {

        $超时订单 = false;
        $auction_success = Db::name('auction_success')->where('id', '=', $auction_success_id)->find();
        if ($auction_success['status'] == 'un' || $auction_success['status'] == 'buy') {
            //状态:un=待操作,post=寄拍,buy=购买,timeout=超时
            if ($auction_success['end_time'] <= time()) {

                $order_item_id = $auction_success['order_item_id'];
                switch ($auction_success['status']) {
                    case 'un':

                        $超时订单 = true;
                        break;

                    case 'buy':
                        if ($order_item_id) {
                            $order_item = Db::name('order_item')->where('id', '=', $order_item_id)->find();
                            if ($order_item['status'] == '0' || $order_item['status'] == '-1') {

                                $超时订单 = true;
                                Db::name('order_item')->where('id', '=', $order_item_id)->update(['status' => '-2', 'updatetime' => time()]);
                                //加入库存
                                $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
                                $tpmecms->IncOrderGoodsStock($order_item_id, '拍卖超时取消总订单');
                            }
                            //$超时订单
                        } else {
                            $超时订单 = true;
                        }


                        break;
                    default:

                        break;
                }



                //修改超时
                if ($超时订单) {
                    //扣除买家的积分或保证金

                    // 如果买家保证金存在就赔偿给卖家,平台首5%的手续费

                    //获取买家的保证金
                    // pay_type	pay_type	12		支付类型:surety=保证金,act_surety=活动保证金,score=积分,shell=贝壳,money=余额
                    // pay_status	pay_status	8		支付状态:2=已支付,3=已解冻,4=已超时
                    $freeze = Db::name('freeze')->where('auction_id', '=', $order_item['activity_id'])->where('user_id', '=', $order_item['user_id'])->where('pay_status', '=', '2')->where('pay_type', '=', 'surety')->find();
                    if ($freeze) {
                        $赔给卖家的保证金 = $tpmecms->扣除拍卖保证金的手续费给卖家($freeze['surety']);
                        $tpmecms->IncStoreMoney($order_item['store_id'], $赔给卖家的保证金, '用户超时未支付,赔给卖家的保证金', 0, $order_item_id);
                    }



                    Db::name('auction_success')->where('id', '=', $auction_success_id)->update(['status' => 'timeout']);


                    Db::name('order_item')->where('id', '=', $order_item_id)->update(['status' => '-2']);
                    \Imi\Log\Log::error('拍卖超时超时,未支付' . $order_item_id);
                }
            }
        }
        //订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
        // if ($order_item['status'] == '0') {
        // }





    }

    /**
     * 创建订单
     *
     * @param int $id 活动id
     * @param int $user_id 用户id
     * @param float $price 中拍价格
     * @return void
     */
    private function createOrder($id, $user_id, $price = null)
    {
        if (!$user_id)
            \Imi\Log\Log::error('创建拍卖订单' . $id . "错误没有用户id" . $user_id . 'price' . $price);

        $this->createAuctionSuccess($id, $user_id, $price);





        // //获取拍卖信息 (用户 商品 )
        // $auction = Db::name('auction')->where('id', '=', $id)->find();
        // $offer = Db::name('auction_offer')->where('auction_id', '=', $id)->where('user_id', '=', $user_id)->orderRaw('id desc')->find();
        // $sku = Db::name('goodssku_price')->where('id', '=', $offer['goods_sku_price_id'])->find();

        // $data['type'] = 'auction';//
        // $data['order_sn'] = rand_order('P' . $auction['type_status'], $user_id);
        // $data['user_id'] = $user_id;
        // $data['activity_type'] = 'auction';
        // $data['goods_amount'] = $auction['now_price']; //获取用户的最终价格
        // if($auction['dispatch_ids']){

        //     $data['dispatch_amount'] = 0; //等用户选择地址后再判断是否需要运费
        // }else{
        //     $data['dispatch_amount'] = 0; //等用户选择地址后再判断是否需要运费
        // }


        // $data['status'] = '0'; //订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
        // $data['goods_original_amount'] = $sku['price']; //商品原价
        // $data['createtime'] = time();
        // $data['store_id'] = $auction['store_id'];
        // $data['id'] = Db::name('order')->insert($data)->getLastInsertId();
        // //创建子订单
        // $item['user_id'] =  $user_id;
        // $item['order_id'] =  $data['id'];
        // $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
        // $goods = $tpmecms->GetGoodsDataById($auction['goods_id']);

        // $item['goodslog_id'] = $tpmecms->getNewGoodslogId($auction['goods_id']);
        // $item['goods_id'] = $auction['goods_id'];
        // $item['goods_type'] = $goods['type'];//activity_type 活动类型
        // $item['activity_type'] = 'auction';



        // $item['dispatch_id'] = $auction['dispatch_ids'];//运费模版

        // $item['goods_sku_price_id'] = $offer['goods_sku_price_id'];
        // $item['goods_sku_text'] = $sku['goods_sku_text'];
        // $item['goods_title'] = $goods['title'];
        // $item['goods_image'] = $goods['image'];
        // $item['goods_original_price'] = $goods['price'];
        // $item['goods_num'] = 1;
        // $item['goods_price'] = $price ? $price : $auction['now_price'];
        // $item['aftersale_status'] = '0';
        // $item['status'] = 0;
        // $item['store_id'] = $goods['store_id'];
        // $item['createtime'] = time();
        // $item['activity_id'] = $id;
        // // Db::name('order_item')->where('')
        // $order_item_id = Db::name('order_item')->insert($item)->getLastInsertId();



        // //定时器 多久后判断是否已经付款,未付款取消订单
        // $timerId2 = \Imi\Timer\Timer::after((int)config('unpay_order_cancel_time') * 1000, function () use ($order_item_id) { //(int)config('unpay_order_cancel_time')

        //     if ($this->bug) var_dump('拍卖超时未支付,订单:order_item_id' . $order_item_id);
        //     $this->订单超时取消订单($order_item_id);
        // });
    }


    public function  订单超时取消订单($order_item_id)
    {

        $order_item = Db::name('order_item')->where('id', '=', $order_item_id)->find();
        //订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
        if ($order_item['status'] == '0') {

            Db::name('order_item')->where('id', '=', $order_item_id)->update(['status' => '-2', 'updatetime' => time()]);
            //加入库存
            $tpmecms = new \ImiApp\ApiServer\Api\Controller\Tpmecms;
            $tpmecms->IncOrderGoodsStock($order_item_id, '拍卖超时取消总订单');


            //扣除买家的积分或保证金

            // 如果买家保证金存在就赔偿给卖家,平台首5%的手续费

            //获取买家的保证金
            // pay_type	pay_type	12		支付类型:surety=保证金,act_surety=活动保证金,score=积分,shell=贝壳,money=余额
            // pay_status	pay_status	8		支付状态:2=已支付,3=已解冻,4=已超时
            $freeze = Db::name('freeze')->where('auction_id', '=', $order_item['activity_id'])->where('user_id', '=', $order_item['user_id'])->where('pay_status', '=', '2')->where('pay_type', '=', 'surety')->find();
            if ($freeze) {
                $赔给卖家的保证金 = $tpmecms->扣除拍卖保证金的手续费给卖家($freeze['surety']);
                $tpmecms->IncStoreMoney($order_item['store_id'], $赔给卖家的保证金, '用户超时未支付,赔给卖家的保证金', 0, $order_item_id);
            }


            Db::name('order_item')->where('id', '=', $order_item_id)->update(['status' => '-2']);
            \Imi\Log\Log::error('拍卖超时超时,未支付' . $order_item_id);
        }



        // $main_order['id'] = $order_id;
        // $order_data = Db::name('order_total')->whereEx($main_order)->find();
        // if ($order_data) {
        //     ////支付状态:1=待支付,2=已支付,3=已退款,4=已超时

        //     if ($order_data['pay_status'] == '1') {
        //         //修改主状态
        //         $update_main['pay_status'] = '4';
        //         Db::name('order_total')->whereEx($main_order)->update($update_main);
        //         //循环取消子订单
        //         $order_list = Db::name('order')->whereIn('id', explode(',', $order_data['order_ids']))->select()->getArray();
        //         foreach ($order_list as $key => $val) {
        //             //
        //             //订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成 
        //             Db::name('order')->where('id', '=', $val['id'])->update(['status' => '-2', 'updatetime' => time()]);

        //             //加入库存
        //             $this->IncOrderGoodsStock($val['id'], '超时取消总订单');
        //         }
        //     } else {
        //         \Imi\Log\Log::error('主订单非待支付状态' . $order_id);
        //     }
        // } else {
        //     \Imi\Log\Log::error('主订单超时,未找到' . $order_id);
        // }


    }
}
